package com.parktech.sun.domain.repository.lucene;

import com.parktech.sun.domain.model.ContentEntity;
import com.parktech.sun.domain.repository.IndexRepository;
import com.parktech.sun.utils.HTMLHelper;
import com.parktech.sun.web.listener.GlobalConfig;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.FSDirectory;
import org.springframework.stereotype.Repository;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

/**
 * User: Think
 * Date: 11-1-22
 * Time: 下午12:03
 */
@Repository
public class IndexRepositoryImpl implements IndexRepository {

    public void updateIndex(ContentEntity entity) {

        try {
            IndexWriter writer = prepareIndexWriter();

            Document document = new Document();
            document.add(new Field("id", entity.getId().toString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.add(new Field("location", entity.getLocation(), Field.Store.YES, Field.Index.NO));
            document.add(new Field("title", entity.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
            document.add(new Field("contentText", HTMLHelper.getContentText(entity.getContent()), Field.Store.YES, Field.Index.ANALYZED));
//        document.add(new Field("content", entity.getContent(), Field.Store.YES, Field.Index.ANALYZED));
            writer.updateDocument(new Term("id", entity.getId().toString()), document);

            writer.optimize();
            writer.close();

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteIndex(ContentEntity entity) {
        deleteIndexById(entity.getId());
    }

    public void deleteIndexById(Long id) {

        try {
            IndexWriter writer = prepareIndexWriter();
            Term term = new Term("id", id.toString());
            writer.deleteDocuments(term);
            writer.optimize();
            writer.close();

        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private File getIndexDir() {
        String indexPath = GlobalConfig.getWebRoot() + "/../index";
        File indexDir = new File(indexPath);
        if (!indexDir.exists()) indexDir.mkdirs();
        return indexDir;
    }

    public void createIndex(ContentEntity entity) {
        try {
        IndexWriter writer = prepareIndexWriter();

        Document document = new Document();
        document.add(new Field("id", entity.getId().toString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        document.add(new Field("location", entity.getLocation(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        document.add(new Field("title", entity.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("contentText", HTMLHelper.getContentText(entity.getContent()), Field.Store.YES, Field.Index.ANALYZED));
//        document.add(new Field("content", entity.getContent(), Field.Store.YES, Field.Index.ANALYZED));
        writer.addDocument(document);

        writer.optimize();
        writer.close();

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private IndexWriter prepareIndexWriter() throws IOException {
        File indexDir = getIndexDir();
        Analyzer analyzer = new IKAnalyzer();
        return new IndexWriter(FSDirectory.open(indexDir), analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
    }

}
